/* index js */
(function (jq, g) {
   jq(document).ready(function(jq) {
      // 获取字符串长度，中文是长度是2
      function getCharsL(_chars) {
         _chars = _chars || '';
         _chars = _chars.replace(/^\n+|\n+$/g, ""); //将富文本中\n过滤
         var _entryLen0 = _chars.length,
            _entryLen = 0;
         var _cnChar = _chars.match(/[^\x00-\x80]/g); //利用match方法检索出中文字符并返回一个存放中文的数组 
         if (!!_cnChar && _cnChar.length > 0)
            _entryLen = _cnChar.length || 0; //算出实际的字符长度
         return _entryLen0 + _entryLen;
      };

      // 截断字符串，处理中英文情况 实际的长度，中文算两个，则传入的_actLength为*2
      function sliceCnAndEns(_chars, _actLength) {
         _chars = _chars || '';
         var _ret = '',
            _length = _chars.length,
            _chars = _chars.split(''),
            _readLength = 0;
         for (var i = 0; i < _length; i++) {
            var _char = _chars[i];
            var _arr = _char.match(/[^\x00-\x80]/g);
            if (!!_arr && _arr.length > 0) {
               _readLength += 2; //中文字符
            } else {
               _readLength += 1; //英文字符
            }
            if (_readLength > _actLength) {
               break;
            }
            _ret += _char;
         }
         return _ret;
      }; 

      function buildSlide(){
         // slide
         for (var i = 0; i < window.slideData.length; i++) {
            jq('.j-slidebtns').append('<a class="swbtn f-ib j-slidebtn"></a>');
         };

         jq.selectShow({
            parent : jq('.j-slidewrap'),
            btns : jq('.j-slidebtns').find('.j-slidebtn'),
            data : window.slideData
         });

      }
   	
      // activity
      function buildAct(data){
         if (data.errno != 0 || !data.data) {
            return;
         };

         var clazzs = ['small', 'small', 'big', 'big', 'small', 'small'];
         var tems = '';

         var list = data.data;

         for (var i = 0; i < list.length; i++) {
               if(!list[i] || !clazzs[i]) return;
            
               var tem = '<a href="" class="j-showqrcode f-fl f-pr item '+ clazzs[i] +'" target="_blank">\
                       <img src="'+ list[i].cover +'" class="pic">\
                       <div class="inner f-pa">\
                           <div class="f-pa innerbg"></div>\
                           <div class="f-pa innerbox">\
                               <h5 class="f-thide tit">'+ list[i].title +'</h5>\
                               <p class="f-thide">'+ list[i].start_time +'</p>\
                               <p class="f-thide" title="'+ list[i].address_info.address +'">'+ list[i].address_info.address +'</p>\
                           </div>\
                       </div>\
                   </a>';

               tems += tem;

         };

         jq('#j-actwrap').append(tems);
      }

      // video
      function playVideo(){
         jq('#j-playbtn').hide();
         jq('#j-videocover').hide();

         var _tpl = '<video id="" width="100%" height="100%" controls="controls" autoplay="autoplay" preload="auto">\
         <source src="http://paintmore.file.alimmdn.com/99.mp4?t=1453882587284" type="video/mp4">Your browser does not support the video tag.</video>';
         
         jq('#j-flashbox').append(_tpl);
      }

      // teacher
      function buildTeacher(data){
         if (data.errno != 0 || !data.data) {
            return;
         };

         var tems = '';
            
         var list = data.data;

         for (var i = 0; i < list.length; i++) { 
            if (getCharsL(list[i].intro) > 200) {
               list[i].intro = sliceCnAndEns(list[i].intro, 200) + '...';
            };

               var tem = '<div class="item f-fl">\
                     <div class="box">\
                        <h5>'+ list[i].name +'</h5>\
                        <p>'+ list[i].intro +'</p>\
                     </div>\
                     <div class="arr"></div>\
                     <div class="face">\
                        <img src="'+ list[i].avatar +'">\
                     </div>\
                  </div>';

               tems += tem;
         };

         jq('#j-teachers').append(tems);
      }

      // qrcode
      function buildQrcode(){
         var temp = '<div class="qrcodewrap">\
                        <div class="qr"><img src="../image/qrcode.png"></div>\
                        <div class="txt f-pr f-f0">\
                           <div class="arr f-pa"></div>\
                           <p>好像此路不通<br>先去微信玩一会吧<p>\
                        </div>\
                     </div>';

         jq(document.body).delegate('.j-showqrcode', 'click', function(e){
               e.preventDefault()

               jq.showlDialog({
                  content:temp,
                  clazz:'m-qrcodedialog'
               });

         });
      }

      function initPage(){
         buildSlide();
         buildQrcode();

         jq('#j-playbtn').click(playVideo);
         jq.getJSON('http://paintmore.cn/api/activity/gets?recommend=index', buildAct);
         jq.getJSON('http://paintmore.cn/api/lecturer/gets?recommend=index', buildTeacher);

      }

      initPage();

   });
})(jQuery, window);